﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Utilities;

namespace NetTopologySuite.Index.Strtree
{
	public class Interval : IIntersectable<Interval>, IExpandable<Interval>
	{
		public Interval(Interval other) : this(other._min, other._max)
		{
		}

		public Interval(double min, double max)
		{
			Assert.IsTrue(min <= max);
			this._min = min;
			this._max = max;
		}

		public double Centre
		{
			get
			{
				return (this._min + this._max) / 2.0;
			}
		}

		public void ExpandToInclude(Interval other)
		{
			this._max = System.Math.Max(this._max, other._max);
			this._min = System.Math.Min(this._min, other._min);
		}

		public Interval ExpandedBy(Interval other)
		{
			this._max = System.Math.Max(this._max, other._max);
			this._min = System.Math.Min(this._min, other._min);
			return this;
		}

		public bool Intersects(Interval other)
		{
			return other._min <= this._max && other._max >= this._min;
		}

		public override bool Equals(object o)
		{
			if (!(o is Interval))
			{
				return false;
			}
			Interval interval = (Interval)o;
			return this._min == interval._min && this._max == interval._max;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		private double _min;

		private double _max;
	}
}
